package com.woniuxy.microcontentserver.advertisemsg.controller;

import com.woniuxy.common.bean.ResultMsg;
import com.woniuxy.microcontentserver.advertisemsg.service.IAdvertiseService;
import com.woniuxy.microcontentserver.bean.AdvertiseBean;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/advertise")
public class AdvertiseController {

    @Resource
    private IAdvertiseService advertiseServiceImpl;

    /**
     * 查询所有广告
     * @param advertiseBean
     * @return
     */
    @GetMapping(value="/all",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultMsg findAll(AdvertiseBean advertiseBean){
        ResultMsg resultMsg = new ResultMsg();
        List<AdvertiseBean> advertiseBeans= advertiseServiceImpl.findAll(advertiseBean);
        if (advertiseBeans == null) {
            return resultMsg.setCode(10000).setMessage("系统繁忙，请稍后再试");
        }
        return resultMsg.setCode(200).setMessage("查询成功").setData(advertiseBeans);
    }

   /* @GetMapping("/findByRoleId")
    ResultMsg findByRoleId( Integer id){
        List<AdminInfo> datas = roleServiceImpl.findByRoleId(id);
        ResultMsg resultMsg = new ResultMsg(true);
        resultMsg.setData(datas);
        return resultMsg;
    }*/

    /**
     * 通过id查询广告
     * @param id
     * @return
     */
   @GetMapping(value="/findbyid",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultMsg getOneById(Long id){
        ResultMsg resultMsg = new ResultMsg();
        AdvertiseBean advertiseBean=advertiseServiceImpl.getOneById(id);
        if (advertiseBean == null) {
            return resultMsg.setCode(10000).setMessage("系统繁忙，请稍后再试");
        }
        return resultMsg.setCode(200).setMessage("查询成功").setData(advertiseBean);
    }

    /*@PostMapping(value = "/categoriesByConditions", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getCategoryByConditions(@RequestBody Category category){
        List<Category> categoriesByConditions = categoryService.getCategoriesByMultiConditions(category);
        ResultMsg resultMsg = new ResultMsg(true);
        resultMsg.setData(categoriesByConditions);

        return JSONObject.toJSONString(resultMsg);
    }*/

    /**
     * 多条件查询
     * @param advertiseBean
     * @return
     */
    @PostMapping(value = "/findByCondition", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultMsg findByCondition(@RequestBody AdvertiseBean advertiseBean){
        ResultMsg resultMsg=new ResultMsg();
        List<AdvertiseBean> advertiseByConditions= advertiseServiceImpl.findByCondition(advertiseBean);
        if (advertiseByConditions == null) {
            return resultMsg.setCode(10000).setMessage("系统繁忙，请稍后再试");
        }
        return resultMsg.setCode(200).setMessage("查询成功").setData(advertiseByConditions);
    }


    /*@PutMapping(value = "/update",produces = MediaType.APPLICATION_JSON_VALUE)
    ResultMsg update(@RequestBody RoleInfo roleInfo){
        System.out.println(roleInfo);
        roleServiceImpl.update(roleInfo);
        return new ResultMsg(true);
    }*/
    @PutMapping(value = "/update",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultMsg update(@RequestBody AdvertiseBean advertiseBean){
        System.out.println(advertiseBean);
        advertiseServiceImpl.updateAdvertiseBean(advertiseBean);
        return new ResultMsg(true);

    }

    /**
     * 新增广告
     * @param advertiseBean
     * @return
     */
    @PostMapping(value = "/add",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultMsg addAdvertiseBean(AdvertiseBean advertiseBean){
        ResultMsg resultMsg=new ResultMsg();
        int addNew=advertiseServiceImpl.addAdvertiseBean(advertiseBean);
        if (addNew == 0) {
            return resultMsg.setCode(10000).setMessage("系统繁忙，请稍后再试");
        }
        return resultMsg.setCode(200).setMessage("查询成功").setData(addNew);
    }

    /**
     * 删除广告
     * @param id
     * @return
     */
    @PutMapping(value = "/delete",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultMsg deleteAdvertiseBean(Long id){
        ResultMsg resultMsg=new ResultMsg();
        int del= advertiseServiceImpl.deleteAdvertiseBean(id);
        if (del == 0) {
            return resultMsg.setCode(10000).setMessage("系统繁忙，请稍后再试");
        }
        return resultMsg.setCode(200).setMessage("查询成功").setData(del);
    }
}
